<?php
namespace Home\Controller;
use Think\Controller;

class BBStypeController extends Controller
{
	public function __construct()
	{
		parent::__construct();
		// 天气接口
		// 转换编码UTF-8至GBK
		$city = iconv('UTF-8','GBK','上海');
		$url = 'http://php.weather.sina.com.cn/xml.php?city='.$city.'&password=DJOYnieT8234jlsK&day=0';
		//CURL的初始化
	    $curl = curl_init();
	    // var_dump($curl);

	    //CURL配置
	    curl_setopt($curl,CURLOPT_URL,$url);
	    curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
	    //curl执行
	    $data = curl_exec($curl);

		//关闭curl
	    curl_close($curl);

	    //处理xml 返回对象
	    $obj = simplexml_load_string($data);
	    $this->assign('obj',$obj);
	}
	//全部板块页面
	public function bbsType()
	{
		
		$type = M('type');
		$fatherMap['fid']= '0';
		$data = $type->where($fatherMap)->select();
		//更改数组，便于HTML页面双重遍历
		foreach ($data as $key => $value) {
			$data[$key]['zilei'] = $type->where("fid={$value['tid']}")->select();
		}

		$this->assign('list',$data);
		$this->display('BBStype/bbstype');
	}


	//按板块查询帖子
	public function bbsView()
	{
		if (empty($_GET['tid'])) {
			$this->redirect('Home/BBStype/bbstype');
			exit;
		}

		$tid = I('get.tid/d');
		$model = M('invitation');
		$type = M('type');
		$rep = M('comment');
		$list = $type->field('tid')->where('fid ='.$tid)->select();
		//判断是否有子类
		if(!empty($list)){
				foreach ($list as $key => $value) {
				$list[$key] = $value['tid']; 
			}
			$list = implode(',', $list);
			$data = $model->field(array('user.username','user.userimage','type.tname','invitation.intitle','user.uid','invitation.inid','invitation.intime','invitation.inpv','type.tid'))->table(array('bbs_user'=>'user','bbs_type'=>'type','bbs_invitation'=>'invitation'))->where('invitation.pid=type.tid and invitation.uid=user.uid and invitation.pid in ('.$list.')')->select();
		}else{
			$data = $model->field(array('user.username','user.userimage','type.tname','invitation.intitle','user.uid','invitation.inid','invitation.intime','invitation.inpv','type.tid'))->table(array('bbs_user'=>'user','bbs_type'=>'type','bbs_invitation'=>'invitation'))->where('invitation.pid=type.tid and invitation.uid=user.uid and invitation.pid='.$tid)->select();
		}

		$time = time();//生成当前时间
		//构造二维数组      查询出每个帖子下面的最新一条评论信息
		foreach ($data as $key => $value) {
			$data[$key]['comment'] = $rep->where("inid = {$value['inid']}")->order('ctime desc')->limit(1)->select();
			$data[$key]['vtime'] = $this->ViewTime($time,$data[$key]['comment']);
			$data[$key]['count'] = $rep->where("inid = {$value['inid']}")->select();
			$data[$key]['count'] = count($data[$key]['count']);
			//增加一个用于排序的数组
			if(!empty($data[$key]['vtime'])){
				$order[$key] = $data[$key]['comment'][0]['ctime'];
			}else{
				$order[$key] = $data[$key]['intime'];
			}
		}
		array_multisort($order, SORT_DESC, $data); 
		$this->assign('data',$data);
		
		$this->display('BBSIndex/bbsindex');

	}
	
	//计算回帖时间距今时间的函数
	public function ViewTime($a,$b){
			if(!empty($b)){
				$ctime = ($a - $b[0]['ctime'])/60;//计算时间差 分钟为单位
				$ctime = (int)$ctime;
			if($ctime<1){
				$vtime = '刚刚';//$vtime用于传值于前端显示的字符型变量，$ctime用于计算的int型变量
			}else{
				$vtime = $ctime.'分钟前';
			}
			//如果大于60分钟
			if($ctime>60){
				$ctime = $ctime/60;//小时为单位
				$ctime = (int)$ctime;
				$vtime = $ctime.'小时前';
				//如果大于24小时
				if($ctime>24){
					$ctime = (int)$ctime;
					$ctime = $ctime/24;//天 为单位
					$ctime = (int)$ctime;
					$vtime = $ctime.'天前';
					//大于7天 直接显示年月日
					if($ctime>7){
						$vtime = date("Y年m月d日",$b[0]['ctime']);
					}
				}
			}
		return $vtime;
		}
	}


}